package com.zhy.baselibrary.utils.photo
import android.content.pm.PackageManager
import android.app.Activity
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import com.blankj.utilcode.util.AppUtils
import com.zhy.baselibrary.utils.dialog.DialogUtils
import io.reactivex.rxjava3.core.Observer
import io.reactivex.rxjava3.disposables.Disposable

class PermissionUtils {
    companion object {
        fun checkLocation(
            mActivity: FragmentActivity,
            msg: String,
            onPermissionListener: OnPermissionListener,
            noPermissionDialog: Boolean = false
        ) {
            if (ContextCompat.checkSelfPermission(
                    mActivity,
                    android.Manifest.permission.ACCESS_FINE_LOCATION
                ) == PackageManager.PERMISSION_DENIED
            ) {
                DialogUtils.initDialog(mActivity)
                    .builder(msg)
                    .showDialog("确定", {
//                        RxPermissions(mActivity)
//                        .request(android.Manifest.permission.ACCESS_FINE_LOCATION)
//                            .subscribe(object : Observer<Boolean> {
//                                override fun onSubscribe(d: Disposable) {
//                                }
//
//                                override fun onNext(t: Boolean) {
//                                    //允许权限
//                                    if (t) {
//                                        onPermissionListener.onPermission(true)
//                                    } else {
//                                        if (noPermissionDialog) {
//                                            jumpDialog(mActivity, "请到设置界面开启\"定位\"权限")
//                                        } else {
//                                            onPermissionListener.onPermission(false)
//                                        }
//                                    }
//                                }
//
//                                override fun onError(e: Throwable) {
//                                }
//
//                                override fun onComplete() {
//                                }
//                            })
                    }, false)
            } else {
                onPermissionListener.onPermission(true)
            }
        }

        private fun jumpDialog(mActivity: Activity, msg: String) {
            DialogUtils.initDialog(mActivity)
                .builder(msg)
                .showDialog("去设置", {
                    AppUtils.launchAppDetailsSettings()
                }, false)
        }

        fun checkStorage(
            mActivity: FragmentActivity,
            msg: String,
            onPermissionListener: OnPermissionListener,
            noPermissionDialog: Boolean = false
        ) {
            if (ContextCompat.checkSelfPermission(
                    mActivity,
                    android.Manifest.permission.WRITE_EXTERNAL_STORAGE
                ) == PackageManager.PERMISSION_DENIED
            ) {
                DialogUtils.initDialog(mActivity)
                    .builder(msg)
                    .showDialog("确定", {
//                        val rxPermissions = RxPermissions(mActivity)
//                        rxPermissions.request(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
//                            .subscribe(object : Observer<Boolean> {
//                                override fun onSubscribe(d: Disposable) {
//                                }
//
//                                override fun onNext(t: Boolean) {
//                                    //允许权限
//                                    if (t) {
//                                        onPermissionListener.onPermission(true)
//                                    } else {
//                                        if (noPermissionDialog) {
//                                            jumpDialog(mActivity, "请到设置界面开启\"存储\"权限")
//                                        } else {
//                                            onPermissionListener.onPermission(false)
//                                        }
//                                    }
//                                }
//
//                                override fun onError(e: Throwable) {
//                                }
//
//                                override fun onComplete() {
//                                }
//                            })
                    }, false)
            } else {
                onPermissionListener.onPermission(true)
            }
        }

        fun checkCallPhone(
            mActivity: FragmentActivity,
            msg: String,
            onPermissionListener: OnPermissionListener,
            noPermissionDialog: Boolean = false
        ) {
            if (ContextCompat.checkSelfPermission(
                    mActivity,
                    android.Manifest.permission.CALL_PHONE
                ) == PackageManager.PERMISSION_DENIED
            ) {
                DialogUtils.initDialog(mActivity)
                    .builder(msg)
                    .showDialog("确定", {
//                        val rxPermissions = RxPermissions(mActivity)
//                        rxPermissions.request(android.Manifest.permission.CALL_PHONE)
//                            .subscribe(object :Observer<Boolean> {
//                                override fun onSubscribe(d: Disposable) {
//                                }
//
//                                override fun onNext(t: Boolean) {
//                                    //允许权限
//                                    if (t) {
//                                        onPermissionListener.onPermission(true)
//                                    } else {
//                                        if (noPermissionDialog) {
//                                            jumpDialog(mActivity, "请到设置界面开启\"拨打电话\"权限")
//                                        } else {
//                                            onPermissionListener.onPermission(false)
//                                        }
//                                    }
//                                }
//
//                                override fun onError(e: Throwable) {
//                                }
//
//                                override fun onComplete() {
//                                }
//                            })
                    }, false)
            } else {
                onPermissionListener.onPermission(true)
            }
        }

        fun checkCamera(
            mActivity: FragmentActivity,
            msg: String,
            onPermissionListener: OnPermissionListener,
            noPermissionDialog: Boolean = false
        ) {
            if (ContextCompat.checkSelfPermission(
                    mActivity,
                    android.Manifest.permission.CAMERA
                ) == PackageManager.PERMISSION_DENIED
            ) {
                DialogUtils.initDialog(mActivity)
                    .builder(msg)
                    .showDialog("确定", {
//                        val rxPermissions = RxPermissions(mActivity)
//                        rxPermissions.request(android.Manifest.permission.CAMERA)
//                            .subscribe(object :Observer<Boolean> {
//                                override fun onSubscribe(d: Disposable) {
//                                }
//
//                                override fun onNext(t: Boolean) {
//                                    //允许权限
//                                    if (t) {
//                                        onPermissionListener.onPermission(true)
//                                    } else {
//                                        if (noPermissionDialog) {
//                                            jumpDialog(mActivity, "请到设置界面开启\"相机\"权限")
//                                        } else {
//                                            onPermissionListener.onPermission(false)
//                                        }
//                                    }
//                                }
//
//                                override fun onError(e: Throwable) {
//                                }
//
//                                override fun onComplete() {
//                                }
//                            })
                    }, false)
            } else {
                onPermissionListener.onPermission(true)
            }
        }


    }

    interface OnPermissionListener {
        fun onPermission(isPermission: Boolean)
    }
}